package com.combo.auth;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import com.combo.util.DES;
import com.combo.util.GsonUtils;

public class ComboAuthManger {

    private Builder mBuilder;
    private ComboActiveReceiver comboActiveReceiver;

    private ComboAuthManger(Builder builder) {
        this.mBuilder = builder;
        comboActiveReceiver = new ComboActiveReceiver();
        comboActiveReceiver.registerReceiver(mBuilder.context);
    }

    public void startAuth() {
        auth();
    }

    private void auth() {
        try {
            String data = GsonUtils.toJson(mBuilder.accountInfoBean);
            String key = "39643588";
            String accountInfo = DES.encryptDES(data, key);
            ComponentName componentName = new ComponentName("com.chinamobile.activate.soft", "com.chinamobile.activate.activity.AutoActivity");
            Intent intent = new Intent();
            //省内版本
            intent.setComponent(componentName);
            intent.putExtra("AccountInfo", accountInfo);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mBuilder.context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void setComboCallback(ComboCallback callback) {
        comboActiveReceiver.setComboCallback(callback);
    }
    public void unregisterReceiver() {
        comboActiveReceiver.unregisterReceiver(mBuilder.context);
    }
    public static class Builder {
        private Context context;
        private AccountInfoBean accountInfoBean;

        public Builder setContext(Context context) {
            this.context = context;
            return this;
        }

        public Builder setAccountInfoBean(AccountInfoBean accountInfoBean) {
            this.accountInfoBean = accountInfoBean;
            return this;
        }

        public ComboAuthManger build() {
            return new ComboAuthManger(this);
        }

    }
}
